/**
 * @author Arvin Lee
 */
Ext.ux.ViewPortPanel = function(){
	// alert(src.windowId);
    var tabPanel = null;
    var desktop = MyDesktop.getDesktop();
	  var winV = desktop.getWindow('file-view-win');
	  
	  //定义一个模板列表对像
      var ds_webSite_select = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
              url: '/admin/webSite.jhtml?method=webSiteList'
          }),
          reader: new Ext.data.JsonReader({
              root: 'results'
          }, [{
              name: 'id'
          }, {
              name: 'websiteName',
              type: 'string'
          }])
      });
	  
	  
	var start ={
	id : 'start-panel',
	title : '欢迎使用',
	layout : 'fit',
	bodyStyle : 'padding:25px',
	html : '<img src="/static/images/app/bg.jpg"/>'
};					
		
        if (!winV) {
		
			
			var css_sm = new Ext.grid.CheckboxSelectionModel();
            var _cssJsonReader = null;
            //碎片管理
         
                _cssJsonReader = new Ext.data.JsonReader({
                    root: 'results'
                    //successProperty : 'success'
                   // totalProperty: 'totalCount'
                }, [{
                    name: 'fileName',
                    mapping: 'fileName'
                }, {
                    name: 'fileLength',
                    mapping: 'fileLength'
                }, {
                    name: 'websiteUrl',
                    mapping: 'websiteUrl'
                }, {
                    name: 'lastModifyTime',
                    mapping: 'lastModifyTime'
                }]);
      
            var css_ds = null;
          
            //碎片管理
         
                css_ds = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: '/admin/file.jhtml?method=list&type=css'
                    }),
                    //
                    reader: _cssJsonReader
                });
            
            css_ds.load({
                params: {
                    type: 'css'
                }
            });
            //定义列头信息
            var css_cm = null;
          
            //碎片管理
       
                css_cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), css_sm, {
                    header: "文件名称",
                    width: 100,
                    sortable: true,
                    dataIndex: 'fileName'
                    //renderer:cover_image
                    //editor: new Ext.form.TextField()
                }, {
                    header: "文件大小(字节)",
                    width: 100,
                    sortable: true,
                    dataIndex: 'fileLength'
                },{
                	header: "所属网站地址", 
                	width: 100, 
                	sortable: true, 
                	dataIndex: 'websiteUrl'
                },{
                    header: "最后修改时间",
                    width: 100,
                    //    sortable: true,
                    dataIndex: 'lastModifyTime'
                }]);
                
         
            
            //开始创建Add面板
            var css_addForm = null;
          
            //碎片管理ADD
            ////////////////////////////////////
                css_addForm = new Ext.FormPanel({
                    layout: 'form',
                    border: false,
                    labelSeparator: '：',
                    frame: true,
                    labelWidth: 80,
                    labelAlign: 'left',
                    buttonAlign: 'right',
                    bodyStyle: 'padding:5px',
                    width: 330,
                    fileUpload: true, //允许上传文件
                    items: [
					{
                        xtype: 'hidden',
                        name: 'type',
						value:'css',
                        //width:200
                        anchor: '96%'
                    }, {
                        
                        xtype: 'combo',
                        fieldLabel: '所属站点',
                        name: 'website_id',
                        valueField: 'id',
                        hiddenName: 'website_id',
                        displayField: 'websiteName',
                        mode: 'remote',
                        store: ds_webSite_select,
                        selectOnFocus: true,
                        editable: false,
                        typeAhead: true,
                        triggerAction: 'all',
                        loadingText: '加载中...',
                        emptyText: '请选择模板'
                    },{
                     xtype: 'textfield',
                        inputType: 'file',
                        fieldLabel: '文件',
                        name: 'file',
                        anchor: '96%'
                    
                    }
					],
                    buttons: [{
                        id: 'btnSave',
                        text: '保 存',
                        handler: function(){
                            if (css_addForm.form.isValid())//客户端先验证数据
                            {
                            
                                //  this.disabled = true; //禁止保存按钮，防止多次提交
                                css_addForm.form.doAction('submit', {
                                    url: '/admin/file.jhtml?method=save',
                                    method: 'post',
                                    waitTitle: '请稍候',
                                    waitMsg: '正在提交表单数据，请稍候...',
                                    success: function(form, action){
                                        Ext.MessageBox.alert("提示", "操作成功");
                                        css_addForm.form.reset();
                                        this.disabled = false;
                                        css_newwin.hide();
                                        css_ds.load({
                                            params: {
                                              type: 'css'
                                            }
                                        });//重新加载css_grid的数据
                                    },
                                    failure: function(){
                                        Ext.MessageBox.alert("提示", "保存成功！！！！");
                                        this.disabled = false;
                                    }
                                })
                            }
                        }
                        
                    }, {
                        text: '取 消',
                        handler: function(){
                            css_newwin.hide();
                        }
                    }]
                });
            
            ///over
            function showcssAddPanel(){
                // create the window on the first click and reuse on subsequent clicks
                css_newwin = new Ext.Window({
                    xtype: 'window',
                    title: '添加信息',
                    buttonAlign: 'right',
                    closable: true,
                    layout: 'fit',
                    modal: 'true',
                    width: 350,
                    height: 180,
                    closeAction: 'hide',
                    plain: true,
                    items: new Ext.TabPanel({
                        activeTab: 0,
                        width: 330,
                        height: 170,
                        defaults: {
                            bodyStyle: 'padding:2px'
                        },
                        xtype: 'tabpanel',
                        plain: true,
                        defaults: {
                            frame: true
                        },
                        closable: true,
                        items: [css_addForm]
                    
                    })
                
                });
                css_newwin.show();
            };
            //开始创建edit面板
            
            var css_editForm = null;
           
            //碎片管理edit
                css_editForm = new Ext.FormPanel({
                    layout: 'form',
                    border: false,
                    labelSeparator: '：',
                    frame: true,
                    labelWidth: 80,
                    labelAlign: 'left',
                    buttonAlign: 'right',
                    bodyStyle: 'padding:5px',
                    width: 630,
                    reader: new Ext.data.JsonReader({
                        root: 'results'
                    }, ['fileName', 'content','type']),
                    items: [{
                        xtype: 'hidden',
                        name: 'fileName',
                        //width:200
                        anchor: '96%'
                    },{
                        xtype: 'hidden',
                        name: 'type',
                        //width:200
                        anchor: '96%'
                    }, {
                    
                        xtype: 'textarea',
                        fieldLabel: '内容',
                        name: 'content',
						height:320,
						allowBlank: false,
                        blankText: '内容不能为空！',
                        anchor: '96%'

                    
                    }],
                    buttons: [{
                        id: 'btnSave',
                        text: '保 存',
                        handler: function(){
                            if (css_editForm.form.isValid())//客户端先验证数据
                            {
                            
                                // this.disabled = true; //禁止保存按钮，防止多次提交
                                css_editForm.form.doAction('submit', {
                                    url: '/admin/file.jhtml?method=updateFile',
                                    method: 'post',
                                    waitTitle: '请稍候',
                                    waitMsg: '正在提交表单数据，请稍候...',
                                    success: function(form, action){
                                        Ext.MessageBox.alert("提示", "数据修改成功!!!");
                                        this.disabled = false;
                                        this.css_newwin1.hide();
                                        css_ds.load({
                                            params: {
                                                 type: 'css'
                                            }
                                        });//重新加载css_grid的数据
                                    },
                                    failure: function(){
                                        Ext.MessageBox.alert("提示", "失败！！！！");
                                        this.disabled = false;
                                    }
                                })
                            }
                        }
                        
                    }, {
                        text: '取 消',
                        handler: function(){
                            css_newwin1.hide();
                        }
                    }]
                });
            
            //编辑信息
            function showCssEditPanel(){
                // create the window on the first click and reuse on subsequent clicks
                css_newwin1 = new Ext.Window({
                    xtype: 'window',
                    title: '编辑信息',
                    buttonAlign: 'right',
                    closable: true,
                    layout: 'fit',
                    modal: 'true',
                    width: 650,
                    height: 480,
                    closeAction: 'hide',
                    plain: true,
                    items: new Ext.TabPanel({
                        activeTab: 0,
                        width: 630,
                        height: 470,
                        defaults: {
                            bodyStyle: 'padding:2px'
                        },
                        xtype: 'tabpanel',
                        plain: true,
                        defaults: {
                            frame: true
                        },
                        closable: true,
                        items: [{
                            title: '基本信息',
                            layout: 'column',
                            cls: 'x-plain',
                            items: [css_editForm]
                        }]
                    })
                
                });
                var row = css_grid.getSelections();
                if (row == 0) {
                    Ext.MessageBox.alert('提示', "请确认是否选中要编辑的信息");
                    return;
                }
              
                //碎片管理
           
                    fName = row[0].get("fileName");
                    urlPath = "/admin/file.jhtml?method=view";
                
                css_editForm.form.load({
                
                    url: urlPath,
                    params: {
                        fName: fName,
						type:'css'
                    },
                    method: 'post',
                    success: function(form, action){
                        //  Ext.MessageBox.alert('提示',"加载成功!");
                    },
                    failure: function(form, action){
                        Ext.MessageBox.alert('提示', "数据加载失败！");
                    }
                    
                });
                css_newwin1.show();
            };
            //创建表格
            var css_grid = new Ext.grid.GridPanel({
                    cm: css_cm,
                    sm: css_sm,
                    border: false,
                    loadMask: { // 加载数据时出险遮罩时状态条上呈现的等待信息
                        msg: "数据加载中，请稍等..."
                    },
                    store: css_ds,
                    viewConfig: {
                        forceFit: true // 自动调整每列的宽度
                    },
                    tbar: [{
                        text: '增加',
                        tooltip: '增加一列',
                        iconCls: 'add',
                        handler: function(){
                            showcssAddPanel();
                            // Ext.MessageBox.confirm('提示框', '您确定要进行该操作？');
                        }
                    }, '-', {
                        text: '删除',
                        tooltip: '删除选中的一列',
                        iconCls: 'remove',
                        handler: function(){
                        
                            //var record = css_grid.getSelectionModel().getSelected();// 返回值为 Record 类型 
                            var rows = css_grid.getSelectionModel().getSelections();// 返回值为 Record 数组 
                            if (rows.length == 0) {
                                Ext.MessageBox.alert('警告', '最少选择一条信息，进行删除!');
                            }
                            else {
                                Ext.MessageBox.confirm('提示框', '您确定要进行该操作？', function(btn){
                                    if (btn == 'yes') {
                                    
                                        if (rows) {
                                            for (var i = 0; i < rows.length; i++) {
                                                var conn = new Ext.data.Connection();
                                                conn.request({
                                                   url: '/admin/file.jhtml?method=remove',
                                                     params: {
													  fileName: rows[i].get("fileName"),
                                                type: 'css'
                                            },
                                                    success: function(resp, opt){
                                                        //css_ds.remove(rows[i]);
                                                    },
                                                    failure: function(resp, opt){
                                                        Ext.Msg.alert('Error', 'Unable to delete movie');
                                                    }
                                                });
                                                //alert(rows[i].get("id")); 
                                                //	css_grid.getStore().remove(rows[i]);
                                                css_ds.remove(rows[i]);
                                                css_ds.load({
                                                     params: {
                                                type: 'css'
                                            }
                                                });//重新加载css_grid的数据
                                            }
                                            
                                        }
                                    }
                                });
                            } // 弹出对话框警告 
                        }
                    }, '-', {
                        text: '修改',
                        tooltip: '修改选择的一行内容',
                        iconCls: 'add',
                        handler: function(){
                            showCssEditPanel();
                            // Ext.MessageBox.confirm('提示框', '您确定要进行该操作？');
                        }
                    }],
                    autoExpandColumn: 3
                
                });
           
		
			winV = desktop.createWindow({
				  id: 'file-view-win',
				title: '文件管理',
				closable: true,
				modal: 'true',
				width: 850,
				height: 550,
				shim: false,
				animCollapse: true,
				constrainHeader: true,
				enableTabScroll: true,
				layout: "border",
			defaults : {
				collapsible : true,
				split : true
			},
			items : [ {
				//title : '哈哈',
				id : 'accordion-panel',
				layout : 'border',
				region : 'west',
				margins : '2 0 5 5',
				width : 200,
				minSize : 200,
				maxSize : 250,
				bodyStyle : 'background-color:#DFE8F6',
				defaults : {
					border : false
				},
				bbar : [{
							text : '快速上传文件',
							iconCls : 'icon-plugin',
							handler : function() {
								showZipAddPanel();
							}
						
				}],

				items : [{
					layout : 'accordion',
					region : 'center',
					items : [{
						title : '导航菜单',
						iconCls : 'icon-nav',
						border : false,
						items : [{
							xtype : 'treepanel',
							id:'cIds',
							border : false,
							rootVisible : false,
							autoScroll : true,
							loader : new Ext.tree.TreeLoader({
								dataUrl : '/static/js/ext/desktop/tree-data-manager.json'
							}),
							root : new Ext.tree.AsyncTreeNode(),
							listeners : {
								'click' : function(n) {
									try {
										var sn = this.selModel.selNode || {};
									
										if (n.leaf && n.id != sn.id) {
											
										

											var categoryId = n.id;
											 
											if(tabPanel==null)
											{
											//	alert('tab null');
											tabPanel = Ext.getCmp('web-panel');}
											var tab = tabPanel.getComponent( categoryId);
											
											  if( tab ){
											  	 tabPanel.setActiveTab(tab);
											  }else{
											  	var grid1 = null;
												
											  //	 alert(categoryId);
											  	cPanelEntity = loadClassReturnEntity(categoryId,345);
											  	grid1 = cPanelEntity.getGrid();
											  	tabPanel.add({
													title :n.text,
													id : categoryId,
													iconCls :'tabs',
													layout :'fit',
													items :grid1,
													closable :true
														}).show();
												//console.log( grid1 );
											
													grid1.store
															.load( {
																params : {
																	start :0,
																	limit :20
																},callback: listError
															});
										}
									
											
											
											
											
											
											
										}
									} catch (e) {
									}
								}
							}
						}]
					}]
				}]
			}, {
				xtype :'tabpanel',
				id : 'web-panel',
				region : 'center',
				margins : '2 5 5 0',
				activeItem : 0,
				border : false,
				items : [start]
			}]
			});
			
				winV.show();
		}
		
		
		 var zip_addForm  = new Ext.FormPanel({
                    layout: 'form',
                    border: false,
                    labelSeparator: '：',
                    frame: true,
                    labelWidth: 80,
                    labelAlign: 'left',
                    buttonAlign: 'right',
                    bodyStyle: 'padding:5px',
                    width: 330,
                    fileUpload: true, //允许上传文件
                    items: [
					  {
                        
                        xtype: 'combo',
                        fieldLabel: '所属站点',
                        name: 'website_id',
                        valueField: 'id',
                        hiddenName: 'website_id',
                        displayField: 'websiteName',
                        mode: 'remote',
                        store: ds_webSite_select,
                        selectOnFocus: true,
                        editable: false,
                        typeAhead: true,
                        triggerAction: 'all',
                        loadingText: '加载中...',
                        emptyText: '请选择模板'
                    },{
                     xtype: 'textfield',
                        inputType: 'file',
                        fieldLabel: 'zip文件',
                        name: 'file',
                        anchor: '96%'
                    
                    }
					],
                    buttons: [{
                        id: 'btnSave',
                        text: '保 存',
                        handler: function(){
                            if (zip_addForm.form.isValid())//客户端先验证数据
                            {
                            
                                //  this.disabled = true; //禁止保存按钮，防止多次提交
                                zip_addForm.form.doAction('submit', {
                                    url: '/admin/file.jhtml?method=saveZip',
                                    method: 'post',
                                    waitTitle: '请稍候',
                                    waitMsg: '正在提交表单数据，请稍候...',
                                    success: function(form, action){
                                        Ext.MessageBox.alert("提示", "上传成功");
                                        zip_addForm.form.reset();
                                        this.disabled = false;
                                        zip_newwin.hide();
                                      
                                    },
                                    failure: function(){
                                        Ext.MessageBox.alert("提示", "上传格式不正确");
                                        this.disabled = false;
                                    }
                                })
                            }
                        }
                        
                    }, {
                        text: '取 消',
                        handler: function(){
                            zip_newwin.hide();
                        }
                    }]
                });
				
				 ///over
            function showZipAddPanel(){
                // create the window on the first click and reuse on subsequent clicks
                zip_newwin = new Ext.Window({
                    xtype: 'window',
                    title: '添加信息',
                    buttonAlign: 'right',
                    closable: true,
                    layout: 'fit',
                    modal: 'true',
                    width: 350,
                    height: 180,
                    closeAction: 'hide',
                    plain: true,
                    items: new Ext.TabPanel({
                        activeTab: 0,
                        width: 330,
                        height: 170,
                        defaults: {
                            bodyStyle: 'padding:2px'
                        },
                        xtype: 'tabpanel',
                        plain: true,
                        defaults: {
                            frame: true
                        },
                        closable: true,
                        items: [zip_addForm]
                    
                    })
                
                });
                zip_newwin.show();
            };
			
		
				
};

